//
//  HLVisitorsView.swift
//  HLWeiBo
//
//  Created by jack on 15/12/5.
//  Copyright © 2015年 jack. All rights reserved.
//

import UIKit
import SnapKit
//访问视图协议
protocol HLVisitorsViewDelegate: NSObjectProtocol {
    
    func visitorLogin()
    func visitorRegister()
}

class HLVisitorsView: UIView {

    //定义代理
    weak var delegate: HLVisitorsViewDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        anima()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUserInfo(imageName: String? , message: String?) {
        if let img = imageName, msg = message {
            cricleView.hidden = true
            iconView.image = UIImage(named: img)
            messageLabel.text = msg
            
        }else{
            anima()
        }
    }
    //设置圆圈动画
    private func anima() {
        let cricleAnima = CABasicAnimation(keyPath: "transform.rotation")
        
        cricleAnima.toValue = 2 * M_PI

        cricleAnima.duration = 20
        
        cricleAnima.repeatCount = MAXFLOAT
        
        cricleAnima.removedOnCompletion = false
        
        cricleView.layer.addAnimation(cricleAnima, forKey: nil)
    }
    // MARK: - 懒加载视图
    //图标
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //圆圈
    private lazy var cricleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //消息的label
    private lazy var messageLabel: UILabel = {
        let  label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人,回这里卡有设么惊喜关注一些人,回这里卡卡那有设么惊喜"
        label.numberOfLines = 0
        label.textAlignment = .Center
     return label
        
    }()
    
    //注册按钮
    lazy var registerButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.addTarget(self, action: "registerBtnClick", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    @objc private func registerBtnClick() {
    
        delegate?.visitorRegister()
    }
    @objc private func loginBtnClick() {
        
        delegate?.visitorLogin()
    }
    //登录按钮
    lazy var loginButton: UIButton = {
        let  button = UIButton()
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.addTarget(self, action: "loginBtnClick", forControlEvents: .TouchUpInside)
        return button
    }()
    
    //阴影
    private lazy var maskIconView: UIImageView = {
        let  image   = UIImage(named: "visitordiscover_feed_mask_smallicon")
        let imageView = UIImageView(image: image)
        return imageView
    }()
}

extension HLVisitorsView {
    
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        //1.添加视图
        addSubview(cricleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //2.添加约束
        //2.1 iconView
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
            
        }
        
        //2.1 circleView
        cricleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }

        //2.3 messageLabel
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cricleView.snp_centerX)
            make.top.equalTo(cricleView.snp_bottom).offset(10)
            make.width.equalTo(224)
        }
        
        // 2.4 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        
        // 2.5 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(registerButton.snp_top)
            make.size.equalTo(CGSizeMake(100, 35))
        }

        //阴影
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self.snp_trailing)
            make.leading.equalTo(self.snp_leading)
            make.bottom.equalTo(registerButton.snp_bottom).offset(35)
        }
    }
}